<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
//个人中心控制器
class CenterController extends Controller{
	//构造函数	
	private $user;
	private $address;
	private $orders;
	public function __init(){
		//实例化表模型
	   $this->user = new \Common\Model\User;
	   $this->address = new \Common\Model\Address;
	   $this->orders = new \Common\Model\Uorders;
	}	
	//个人中心首页展示
	public function index(){
		//获取当前登录用的信息
		$userData = $this->user->where("uid={$_SESSION['uid']}")->find();
		//将地址字符串拆成数组
		$zones = explode(',', $userData['adres']);
		$userData['adres'] = $zones;
		//将生日字符串拆分成数组
		$userData['birth'] = explode(',', $userData['birth']);
		//地址详情		
		$userData['detail'] = $zones[3];
		View::with('userData',$userData);
	    View::make();
	}	
	//我的订单展示
	public function myindent(){
		//1.获取当前登录用的信息
		$userData = $this->user->where("uid={$_SESSION['uid']}")->find();
		//将地址字符串拆成数组
		$zones = explode(',', $userData['adres']);
		$userData['adres'] = $zones;
		//将生日字符串拆分成数组
		$userData['birth'] = explode(',', $userData['birth']);
		//地址详情		
		$userData['detail'] = $zones[3];
		View::with('userData',$userData);
		//2.查询订单(订单表关联订单列表)数据
		$model = new \Common\Model\Orderlist;
		$data = $model->join('uorders','oid','=','order_oid')->where("user_uid={$_SESSION['uid']}")->get();
		$new = array();//先定义一个空数组
		foreach ($data as $k => $v) {	//重组新的数组压入空数组
				$num['ordernum'] = $v['ordernum'];
				$num['time'] = $v['buildtime'];
				$num['totalprice'] = $v['totalprice'];
				$num['detail'] = Db::table('orderlist')->join('goods','gid','=','goods_gid')->join('uorders','oid','=','order_oid')->where("order_oid={$v['oid']} AND user_uid={$_SESSION['uid']}")->get();
				$new[] = $num;
		}
		foreach ($new as $kk => $vv) {//将规格属性拆分成数组
			foreach ($vv['detail'] as $key => $value) {
				$new[$kk]['detail'][$key]['remark']= explode(',', $value['remark']);
			}
		}
		View::with('new',$new);
	    View::make();
	}
	//我的收货地址
	public function myaddress(){
		//获取当前登录用的信息
		$userData = $this->user->where("uid={$_SESSION['uid']}")->find();
		//获取登录用户的收货地址数据
		$addressData = $this->address->where("user_uid={$_SESSION['uid']}")->get();
		//重组收货地址数据
		foreach ($addressData as $k => $v) {
			//给电话号码加密
			$addressData[$k]['telephone'] = substr_replace($v['telephone'], '****', 4,4);
			//获得省市区
			$addressData[$k]['zone'] = str_replace(',', '',substr($v['addresses'],0,strrpos($v['addresses'],',')));
			//获得详细地址
			$addressData[$k]['detail'] = substr_replace(ltrim(strrchr($v['addresses'],','),','),"***",0,9);
		}
		//分配数据
		View::with('addressData',$addressData);
		View::with('userData',$userData);
	    View::make();
	}
	//用户资料
	public function edit(){
	    if(IS_POST){
	        if($this->user->edit()) View::success('保存成功',U('Center/index'));
			View::error($this->user->getError());
	        
	    }
	}
	//用户添加新的地址
	public function addnewaddress(){
	    if(IS_POST){
	       if(!$this->address->store()) View::error($this->address->getError());
	       View::success('添加成功',U('Center/myaddress'));
	    }
	}
	//用户删除地址
	public function del(){	   
		   //获得用户did
		   $did = Q("get.did");
		   $this->address->where("did={$did}")->delete();
		   View::success('删除成功',U('Center/myaddress'));	   
	}
	//异步设为默认地址
	public function setAddress(){
			if(IS_POST){
			   //获得用户did(点击之后异步发送过来的)
				$did = Q("post.id");
				//把原来是默认的改为非默认地址
				$this->address->where("user_uid={$_SESSION['uid']}")->save(array('statu'=>0));
				//把用户点击的改为默认地址
				$this->address->where("did={$did} AND user_uid={$_SESSION['uid']}")->save(array('statu'=>1));
				//返出数据
				echo json_encode(array('num'=>1));
			}
	    
	}
	//用户点击修改地址时，异步查询旧的地址数据
	public function queryOldAddress(){
	    if(IS_POST){
	       //获得用户did(点击之后异步发送过来的)
	       $did = Q("post.id");
		   //查询用户要修改的旧地址
		   $oldAddress = $this->address->where("did={$did}")->find();
		   //将以逗号连接的地址以逗号拆分开
		   $oldAddress['addresses'] = explode(',',$oldAddress['addresses']);
//		   p($oldAddress);
		   //返出数据
		   if($oldAddress){
		   		echo json_encode($oldAddress);exit;
		   }else{
		   		echo json_encode(FALSE);exit;
		   }
	        
	    }
	}
	
	//用户修改收货地址
	public function editAddress(){
	    if(IS_POST){
	        $did = Q("post.did");
			if(!$this->address->edit()) View::error($this->address->getError());
			if($_POST['statu'] == 1){//如果选择了设为默认地址
				$this->address->where("user_uid={$_SESSION['uid']}")->save(array('statu'=>0));
				$this->address->where("did={$did} AND user_uid={$_SESSION['uid']}")->save(array('statu'=>1));
			}
			View::success('修改成功',U('Center/myaddress'));
	    }
		
	}
	
}






?>	